/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : MadMinSum.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Feb 12, 2009  Raghu Sarraff	 		        Created
 */
package lwf.handler.entities.math;

import java.util.List;

import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.number.util.NumberFormatter;


public class MadMinSum extends Sum {
	
	private int rownumber=0;
	private int sumnumber=0;
	private String answerbox;    //This represents the name of the answerbox on the UI
	private String quotientbox;  //This represents the name of the answerbox on the UI
	private String remainderbox; //This represents the name of the answerbox on the UI
	private String useranswer;
	private String useranswerquotient;
	private String useranswerremainder;
	private int decimalpoints;
	private boolean isCorrect = false; //for score
	private boolean startRow = false;
	private boolean endRow = false;
	private String displayUserAnswer = "0";
	private String displayActualAnswer = "0";
	
	/**
	 * @return Returns the isNewRow.
	 */
	public boolean getstartRow() {
		return startRow;
	}
	
	
	/**
	 * @param endRow The endRow to set.
	 */
	public void setEndRow(boolean endRow) {
		this.endRow = endRow;
	}
	/**
	 * @return Returns the endRow.
	 */
	public boolean getEndRow() {
		return endRow;
	}
	//to go to a common place
	private String displayFormat() {
		
		String format = "###,###";
		if(decimalpoints != 0) {
			format = format + ".";
			for(int i=0; i<decimalpoints; i++) {
				format = format + "#";
			}
		}
		return format;
	}

	/**
	 * @return Returns the correct.
	 */
	public boolean getIsCorrect() {
		
		if(!getOperation().equals(IHandlerConstant.DIVISION)) {
			if(this.useranswer == null) {
				return false;
			}
			this.displayUserAnswer = NumberFormatter.format(this.useranswer, displayFormat());
			this.displayActualAnswer = NumberFormatter.format(String.valueOf(getAnswer()), displayFormat());
			if(this.displayUserAnswer.equals(this.displayActualAnswer)) {
				return true;
			}
		}
		else {
			int user_quotient = Integer.parseInt(this.useranswerquotient);
			int user_remainder = Integer.parseInt(this.useranswerremainder);
			this.displayUserAnswer = "Quotient: " + this.useranswerquotient + "  and  Remainder: " + this.useranswerremainder;
			double[] actual_answers = (double[]) getAnswer();
			this.displayActualAnswer = "Quotient: " + actual_answers[0] + "  and  Remainder: " + actual_answers[1];
			if(user_quotient == actual_answers[0] && user_remainder == actual_answers[1]) {
				return true;
			}
		}
		return isCorrect;
	}
	

	
	/**
	 * @return Returns the displayActualAnswer.
	 */
	public String getDisplayActualAnswer() {
		return displayActualAnswer;
	}
	/**
	 * @return Returns the displayUserAnswer.
	 */
	public String getDisplayUserAnswer() {
		return displayUserAnswer;
	}
	
	public MadMinSum(String operation, int rownumber, int sumnumber, int decimalpoints) {
		super(operation, decimalpoints);
		this.rownumber = rownumber;
		this.sumnumber = sumnumber;
		this.decimalpoints = decimalpoints;
		if(!operation.equals(IHandlerConstant.DIVISION)) {
			this.answerbox = "answer" + String.valueOf(this.rownumber) + String.valueOf(this.sumnumber);
		}
		else {
		    this.remainderbox = "reminder" + String.valueOf(this.rownumber) + String.valueOf(this.sumnumber); 
		    this.quotientbox = "quotient" + String.valueOf(this.rownumber) + String.valueOf(this.sumnumber);
		}
		if(this.sumnumber-1 == 0) {
			startRow = true;
		}
	}
	
	/**
	 * @return Returns the answerbox.
	 */
	public String getAnswerbox() {
		return answerbox;
	}
	/**
	 * @return Returns the quotientbox.
	 */
	public String getQuotientbox() {
		return quotientbox;
	}
	/**
	 * @return Returns the remainderbox.
	 */
	public String getRemainderbox() {
		return remainderbox;
	}

	public String getHtmlData() {
		StringBuffer sbHtml = new StringBuffer();
		sbHtml.append("<table border=\"0\" align=\"center\" cellspacing=\"0\" valign=\"top\" cellpadding=\"0\">");
		List<Double> data = getData();
			for(int i=data.size(); i>0; i--) {
				sbHtml.append("<tr>");
				    if(i-1 == 0) {
				    	sbHtml.append("<td class=\"question\">" + SumUtil.getOperationSign(getOperation()) + "</td>");	
				    }
				    else {
				    	sbHtml.append("<td>&nbsp;</td>");
				    }
				    String displaydata = String.valueOf(data.get(i-1));
				    //NumberFormatter.format(displaydata, displayFormat())
				    sbHtml.append("<td class=\"data\">" + NumberFormatter.format(displaydata, displayFormat()) + "</td>");
				sbHtml.append("</tr>");
			}
			sbHtml.append("<tr>");
				sbHtml.append("<td colspan=\"2\" class=\"answercell\" nowrap>");

				    if(getOperation().equals(IHandlerConstant.DIVISION)) {
				    	sbHtml.append("<table><tr>");
					    	sbHtml.append("<td><b>quotient</b><br><input type=\"text\" style=\"width:50px\" class=\"answerbox\" name=\"" + this.quotientbox + "\" /> </td>");
					    	sbHtml.append("<td><b>reminder</b><br><input type=\"text\" style=\"width:50px\" class=\"answerbox\" name=\"" + this.remainderbox + "\" /> </td>");
					    sbHtml.append("</tr></table>");
				    }
				    else {
					    sbHtml.append("<input type=\"text\" style=\"width:75px\" class=\"answerbox\" name=\"" + this.answerbox + "\" />");				    	
				    }
				sbHtml.append("</td>");
			sbHtml.append("</tr>");
		sbHtml.append("</table>");
		return sbHtml.toString();
	}
	
	public String getQuestion() {
		StringBuffer sbQ = new StringBuffer();
		
		for(int i=this.getData().size(); i>0; i--) {
			String data = String.valueOf(getData().get(i-1));
			sbQ.append(NumberFormatter.format(data, displayFormat()));	
			if(i-1 != 0) {
				sbQ.append(" " + SumUtil.getOperationSign(getOperation()) + " ");
			}
		}
		sbQ.append(" = ");
		return sbQ.toString();
	}
	
	public void printUserAnswers() {
		System.out.println("========> User answers");
		if(!getOperation().equals(IHandlerConstant.DIVISION)) {
			System.out.println(" User Answer: " + this.useranswer);
		}
		else {
			System.out.println("Quotient: " + this.useranswerquotient + " AND Remainder: " + this.useranswerremainder);
		}
		
	}
	
	/**
	 * @return Returns the useranswer.
	 */
	public String getUseranswer() {
		return useranswer;
	}
	/**
	 * @param useranswer The useranswer to set.
	 */
	public void setUseranswer(String useranswer) {
		if(useranswer == null || useranswer.trim().length() == 0) {
			this.useranswer = "0";
		}
		else {
			this.useranswer = useranswer.trim();	
		}
	}
	/**
	 * @return Returns the useranswerquotient.
	 */
	public String getUseranswerquotient() {
		return useranswerquotient;
	}
	/**
	 * @param useranswerquotient The useranswerquotient to set.
	 */
	public void setUseranswerquotient(String useranswerquotient) {
		if(useranswerquotient == null || useranswerquotient.trim().length() == 0) {
			this.useranswerquotient = "0";
		}
		else {
			this.useranswerquotient = useranswerquotient.trim();	
		}
	}
	/**
	 * @return Returns the useranswerremainder.
	 */
	public String getUseranswerremainder() {
		return useranswerremainder;
	}
	/**
	 * @param useranswerremainder The useranswerremainder to set.
	 */
	public void setUseranswerremainder(String useranswerremainder) {
		if(useranswerremainder == null || useranswerremainder.trim().length() == 0) {
			this.useranswerremainder = "0";
		}
		else {
			this.useranswerremainder = useranswerremainder.trim();	
		}
	}
}

